package com.rrd.itext.xmlBean.layoutBean;

import java.util.ArrayList;
import java.util.List;

import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.CMYKColor;
import com.itextpdf.text.pdf.PdfPCell;
import com.rrd.itext.utils.PDFBuilder;
import com.rrd.itext.utils.StyleUtil;
import com.rrd.itext.xmlBean.layoutBean.iface.PdfElementParser;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("Td")
public class XmlTd extends XmlFontStyle implements PdfElementParser<Rectangle> {
	@XStreamAsAttribute
	@XStreamAlias("colspan")
	private int colspan;
	@XStreamAsAttribute
	@XStreamAlias("rowspan")
	private int rowspan;
	@XStreamAsAttribute
	@XStreamAlias("wrap")
	private boolean wrap;
	@XStreamAsAttribute
	@XStreamAlias("cellHeight")
	private float cellHeight;
	@XStreamAsAttribute
	@XStreamAlias("left")
	private float left;
	@XStreamAsAttribute
	@XStreamAlias("top")
	private float top;
	@XStreamAsAttribute
	@XStreamAlias("border")
	private Float border;
	@XStreamAsAttribute
	@XStreamAlias("leftBorder")
	private	Float leftBorder;
	@XStreamAsAttribute
	@XStreamAlias("rightBorder")
	private Float rightBorder;
	@XStreamAsAttribute
	@XStreamAlias("topBorder")
	private Float topBorder;
	@XStreamAsAttribute
	@XStreamAlias("bottomBorder")
	private Float bottomBorder;
	@XStreamAlias("HTMLBlock")
	private XmlHtmlBlock htmlBlock;
	@XStreamAlias("Image")
	private XmlImage image;
	@XStreamAlias("Paragraph")
	private XmlParagraph paragraph;
	@XStreamAlias("Phrases")
	private List<XmlPhrase> phrases;


	public XmlParagraph getParagraph()
	{
		return paragraph;
	}

	public void setParagraph(XmlParagraph paragraph)
	{
		this.paragraph = paragraph;
	}

	public XmlHtmlBlock getHtmlBlock()
	{
		return htmlBlock;
	}

	public void setHtmlBlock(XmlHtmlBlock htmlBlock)
	{
		this.htmlBlock = htmlBlock;
	}

	public XmlImage getImage()
	{
		return image;
	}

	public void setImage(XmlImage image)
	{
		this.image = image;
	}

	
	public Float getBorder()
	{
		return border;
	}

	public void setBorder(Float border)
	{
		this.border = border;
	}

	public Float getLeftBorder()
	{
		return leftBorder;
	}

	public void setLeftBorder(Float leftBorder)
	{
		this.leftBorder = leftBorder;
	}

	public Float getRightBorder()
	{
		return rightBorder;
	}

	public void setRightBorder(Float rightBorder)
	{
		this.rightBorder = rightBorder;
	}

	public Float getTopBorder()
	{
		return topBorder;
	}

	public void setTopBorder(Float topBorder)
	{
		this.topBorder = topBorder;
	}

	public Float getBottomBorder()
	{
		return bottomBorder;
	}

	public void setBottomBorder(Float bottomBorder)
	{
		this.bottomBorder = bottomBorder;
	}

	public float getLeft()
	{
		return left;
	}

	public void setLeft(float left)
	{
		this.left = left;
	}

	public float getTop()
	{
		return top;
	}

	public void setTop(float top)
	{
		this.top = top;
	}

	public float getCellHeight()
	{
		return cellHeight;
	}

	public void setCellHeight(float cellHeight)
	{
		this.cellHeight = cellHeight;
	}

	public List<XmlPhrase> getPhrases() {
		return phrases==null?new ArrayList<XmlPhrase>():phrases;
	}

	public void setPhrases(List<XmlPhrase> phrases) {
		this.phrases = phrases;
	}

	public boolean getWrap() {
		return this.wrap;
	}

	public void setWrap(boolean wrap) {
		this.wrap = wrap;
	}

	public int getColspan() {
		return colspan;
	}

	public void setColspan(int colspan) {
		this.colspan = colspan;
	}

	public int getRowspan() {
		return rowspan;
	}

	public void setRowspan(int rowspan) {
		this.rowspan = rowspan;
	}

	@Override
	public PdfPCell parse(PDFBuilder builder) throws Exception
	{
		PdfPCell cell = new PdfPCell();
		if(this.getRowspan() > 0)
		{
			cell.setRowspan(this.getRowspan());
		}
		if(this.getColspan() > 0)
		{
			cell.setColspan(this.getColspan());
		}
		
		cell.setUseAscender(true);
		cell.setUseDescender(true);
		
		if(this.getTop() >= 0 && this.getLeft() >= 0)
		{
			cell.setPaddingLeft(this.getLeft());
			cell.setPaddingTop(this.getTop());
		}
		if(this.getCellHeight() > 0)
		{
			cell.setFixedHeight(this.getCellHeight());
		}
		if(this.getBorder() != null && this.getBorder() >= 0)
		{
			cell.setBorderWidth(this.getBorder());
		}
		
		if(this.getLeftBorder() != null && this.getLeftBorder() >= 0)
		{
			cell.setBorderWidthLeft(this.getLeftBorder());
		}
		if(this.getRightBorder() != null && this.getRightBorder() >= 0)
		{
			cell.setBorderWidthRight(this.getRightBorder());
		}
		if(this.getBottomBorder() != null && this.getBottomBorder() >= 0)
		{
			cell.setBorderWidthBottom(this.getBottomBorder());
		}
		if(this.getTopBorder() != null && this.getTopBorder() >= 0)
		{
			cell.setBorderWidthTop(this.getTopBorder());
		}
		
		if(this.getBackgroundColor()!=null){
			  XmlColor color = StyleUtil.createColor(this,this.getBackgroundColor(), builder.getStyleMap());
				int colorC = (int)(((color.getFontColorC()==null?0:color.getFontColorC())/100F)*255);
				int colorM = (int)(((color.getFontColorM()==null?0:color.getFontColorM())/100F)*255);
				int colorY = (int)(((color.getFontColorY()==null?0:color.getFontColorY())/100F)*255);
				int colorK = (int)(((color.getFontColorK()==null?0:color.getFontColorK())/100F)*255);
				cell.setBackgroundColor((new CMYKColor(colorC,colorM,colorY,colorK)));
		}
		if(this.getBackgroundColor()!=null){
			  XmlColor color = StyleUtil.createColor(this,this.getBorderColor(), builder.getStyleMap());
				int colorC = (int)(((color.getFontColorC()==null?0:color.getFontColorC())/100F)*255);
				int colorM = (int)(((color.getFontColorM()==null?0:color.getFontColorM())/100F)*255);
				int colorY = (int)(((color.getFontColorY()==null?0:color.getFontColorY())/100F)*255);
				int colorK = (int)(((color.getFontColorK()==null?0:color.getFontColorK())/100F)*255);
				cell.setBorderColor((new CMYKColor(colorC,colorM,colorY,colorK)));
		}
		
		List<XmlPhrase> xmlPhrases=this.getPhrases();
		if(xmlPhrases != null)
		{
			for (XmlPhrase phrase:xmlPhrases) 
			{
				cell.addElement((Element) phrase.parse(builder));
			}
		}
		
		if (this.getImage() != null) {
			XmlImage image = this.getImage();
            cell.addElement(image.parse(builder));
		}
        if(this.getHtmlBlock()!=null){
        	XmlHtmlBlock htmlBlock=this.getHtmlBlock();
        	cell.addElement((Element) htmlBlock.parse(builder).get(0));
        }
		
		return cell;
	}


}
